﻿using System;
using SimpleServiceBus.Tests.Stubs;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace SimpleServiceBus.Tests.Utilities
{
    [TestFixture]
    public class TimerServiceTestFixture
    {
        [Test]
        public void SerializedTimerTest()
        {
            using (var timerService = new TestTimerService(TimeSpan.FromMilliseconds(10)))
            {
                timerService.PreventConcurrentIntervalProcessing = true;
                timerService.SleepDuration = TimeSpan.FromMilliseconds((100));
                timerService.Counter = 0;

                timerService.Start();
                timerService.Wait.WaitOne();
                //The original interval work
                long lastCount = timerService.Counter;

                Assert.That(lastCount, Is.GreaterThanOrEqualTo((1L)));
                Assert.That(lastCount, Is.LessThanOrEqualTo(2L));

                timerService.Stop();
                //The pended interval work
                Assert.That(timerService.Counter, Is.LessThanOrEqualTo(3L));
            }
        }

        [Test]
        public void TimerTest()
        {
            using (var timerService = new TestTimerService(TimeSpan.FromMilliseconds(100)))
            {
                timerService.Start();
                timerService.Wait.WaitOne();
                timerService.Stop();

                Assert.That(timerService.Duration.Milliseconds, Is.GreaterThanOrEqualTo(100));
                Assert.That(timerService.Duration.Milliseconds, Is.LessThanOrEqualTo(150));
            }
        }
    }
}